import { SUCCESS_CODE } from '@/core/constants/coreConstants'
import { R } from '@/components/axios/interface'
import ApiConfigDto from '@/core/framework/config/types/model/ApiConfigDto'
import { MockMethod } from 'vite-plugin-mock'

class ServerApiMockUtils {
	getDefaultServerApi = (serverApiConfigModel: any, content: any[], timeout: number = 50): any[] => {
		const searchApiConfig: ApiConfigDto = serverApiConfigModel['search']
		const addApiConfig: ApiConfigDto = serverApiConfigModel['add']
		const modifyNotNullFieldApiConfig: ApiConfigDto = serverApiConfigModel['modifyNotNullField']
		const getApiConfig: ApiConfigDto = serverApiConfigModel['get']
		const listApiConfig: ApiConfigDto = serverApiConfigModel['list']
		const removeLogicApiConfig: ApiConfigDto = serverApiConfigModel['list']

		return [
			// 搜索接口
			{
				url: searchApiConfig.mockUri,
				method: searchApiConfig.method,
				timeout,
				response: ({ body }: any) => {
					return {
						data: {
							content: content,
							totalElements: 171,
						},
						code: 0,
						message: '成功',
					}
				},
			},
			{
				url: addApiConfig.mockUri,
				method: addApiConfig.method,
				timeout,
				response: ({ body }: any) => {
					const rnd = Math.floor(Math.random() * 2) + 1
					if (rnd === 1) {
						return {
							code: SUCCESS_CODE,
							message: '成功',
						} as R
					} else {
						return {
							code: 1,
							message: '保存错误',
						} as R
					}
				},
			},
			{
				url: modifyNotNullFieldApiConfig.mockUri,
				method: modifyNotNullFieldApiConfig.method,
				timeout,
				response: ({ body }: any) => {
					const rnd = Math.floor(Math.random() * 2) + 1
					if (rnd === 1) {
						return {
							code: SUCCESS_CODE,
							message: '成功',
						} as R
					} else {
						return {
							code: 1,
							message: '修改错误',
						} as R
					}
				},
			},
			{
				url: getApiConfig.mockUri,
				method: getApiConfig.method,
				timeout,
				response: ({ body, query }: any) => {
					const id = query['id']
					const item = content.find(value => value.id === id)
					return {
						code: SUCCESS_CODE,
						message: '成功',
						data: item,
					} as R
				},
			} as MockMethod,
			{
				url: listApiConfig.mockUri,
				method: listApiConfig.method,
				timeout,
				response: ({ body }: any) => {
					return {
						data: content,
						code: 0,
						message: '成功',
					}
				},
			},
			{
				url: removeLogicApiConfig.mockUri,
				method: removeLogicApiConfig.method,
				timeout,
				response: ({ body }: any) => {
					const rnd = Math.floor(Math.random() * 2) + 1
					if (rnd === 1) {
						return {
							code: SUCCESS_CODE,
							message: '成功',
						} as R
					} else {
						return {
							code: 1,
							message: '删除错误',
						} as R
					}
				},
			},
		]
	}
}

export default new ServerApiMockUtils()
